package zdream.pmfield.stage.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import zdream.pmfield.stage.object.FoeActor;
import zdream.pmfield.stage.object.Seat;

/**
 * 谁发起了攻击、发起攻击打了谁, 这些内容放到 {@link #action} 中
 *
 * @author Zdream
 * @date 2023-11-15
 * @since 0.0.1
 */
public class AttackReport extends StageReport {
	public final List<AttackReportItem> items = new ArrayList<>();

	public AttackReport(AttackAction action, float time) {
		super(action, time);
	}

	@Override
	public AttackAction getAction() {
		return (AttackAction) action;
	}

	public static class AttackReportItem {
		public final FoeActor foe;
		public final Seat seat;
		public AttackReportItem(FoeActor foe, Seat seat) {
			this.foe = Objects.requireNonNull(foe);
			this.seat = Objects.requireNonNull(seat);
		}

		public boolean miss;
		public int damage;
		public int hpFrom, hpTo;
		public int shieldFrom, shieldTo;
		public float typeFixed, attackFixed, defenseFixed, powerFixed;

		public AttackReportItem withMiss(boolean miss) {
			this.miss = miss;
			return this;
		}

		public AttackReportItem withDamage(int damage) {
			this.damage = damage;
			return this;
		}

		public AttackReportItem withHpChange(int from, int to) {
			this.hpFrom = from;
			this.hpTo = to;
			return this;
		}

		public AttackReportItem withShieldChange(int from, int to) {
			this.shieldFrom = from;
			this.shieldTo = to;
			return this;
		}

		public AttackReportItem withTypeFixed(float typeFixed) {
			this.typeFixed = typeFixed;
			return this;
		}

		public AttackReportItem withEffectFixedRate(float attackFixed, float defenseFixed, float powerFixed) {
			this.attackFixed = attackFixed;
			this.defenseFixed = defenseFixed;
			this.powerFixed = powerFixed;
			return this;
		}

		@Override
		public String toString() {
			StringBuilder builder = new StringBuilder(128);
			seat.appendStringWithoutFoe(builder);
			builder.append(' ').append("位置上的").append(' ');
			foe.appendStringWithoutHp(builder);
			builder.append(' ').append("受到了").append(' ').append(damage).append(' ').append("点伤害").append('\n');
			builder.append("HP:").append(' ').append(hpFrom).append(' ').append("->").append(' ').append(hpTo)
					.append('\n');
			builder.append("护盾:").append(' ').append(shieldFrom).append(' ').append("->").append(' ').append(shieldTo)
					.append('\n');
			builder.append("命中:").append(' ').append(miss ? "没有命中" : "命中了").append('\n');
			builder.append("属性克制:").append(' ').append(typeFixed).append('\n');
			builder.append("效果倍率(攻方):").append(' ').append(attackFixed).append('\n');
			builder.append("效果倍率(技能):").append(' ').append(powerFixed).append('\n');
			builder.append("效果倍率(防方):").append(' ').append(defenseFixed).append('\n');
			return builder.toString();
		}
	}

	public AttackReportItem createDamageReport(FoeActor foe) {
		AttackReportItem item = new AttackReportItem(foe, foe.seat);
		this.items.add(item);
		return item;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder(512);

		final AttackAction action = this.getAction();
		builder.append("发动时间: ").append(time).append('\n');
		builder.append("攻击方: ");
		action.foe.appendStringWithoutHp(builder);
		builder.append('\n');
		builder.append("使用技能: ").append(action.move).append('\n');
		builder.append("发动目标: ");
		if (action.targetFoe != null) {
			action.targetFoe.appendStringWithoutHp(builder);
		} else if (action.targetSeat != null) {
			builder.append("位置").append(' ');
			action.targetSeat.appendStringWithoutFoe(builder);
		} else {
			builder.append("不知道哪里");
		}
		builder.append('\n');
		builder.append("结果如下:");

		if (items.isEmpty()) {
			builder.append('\n').append("  <空>");
		}

		int index = 1;
		for (AttackReportItem item : items) {
			builder.append('\n').append("  ").append(index).append(": ");
			String text = item.toString().trim().replace("\n", "\n     ");
			builder.append(text);
		}

		return builder.toString();
	}
}
